<?php

namespace util;

//存放各类容器的容器 单例
class DiStore
{
    private $di;
    private static $instance;

    public static function getInstance(){
        if (!isset(self::$instance)){
            self::$instance = new self;
        }
        return self::$instance;
    }

    private function __construct()
    {
        $this->di = new Di;
        $this->di->set('Di',"util\Di");
        $this->setBuilder();
    }

    private function setBuilder(){
        $this->setDedicatedDiBuilder('HttpHandleDiBuilder', "util\HttpHandleDiBuilder");
        $this->setDedicatedDiBuilder('ControllerDiBuilder', "util\ControllerDiBuilder");
        $this->setDedicatedDiBuilder('BusinessDiBuilder', "util\BusinessDiBuilder");
    }

    private function setDedicatedDiBuilder($diKeyOfBuilder,$builderOfClassName){
        $this->di->set($diKeyOfBuilder,$builderOfClassName)
            ->setArguments(array(new DiMock('Di')))
        ;
    }

    public function getDi($name){
        if (!empty($builder = $this->di[$name.'Builder'])){
            return $builder->get();
        }else{
            return null;
        }
    }
}